// import Static from './static.js'; // 导入参数
/**
 * 统一的音效管理器
 */
export default class Set {
	static instance = null; // 静态属性保存单例

	constructor() {
		if (Set.instance) return Set.instance;
		Set.instance = this;
		this.setShow = true
		this.setBg = { x: 0, y: 0, w: 300, h: 400 }
		// 开关按钮配置（y坐标相对面板顶部）
		this.buttonSettings = [
			{ label: '游戏音效', x: 185, y: 170, w: 80, h: 40, state: false },
			{ label: '其他音乐', x: 185, y: 240, w: 80, h: 40, state: true },
			{ label: '背景音乐', x: 185, y: 100, w: 80, h: 40, state: true },
		];
		return Set.instance;
	}
	// 点击开关按钮
	cikSetBtn1(x, y) {
		const canvas = GameGlobal.databus.canvas
		const ctx = GameGlobal.databus.ctx
		const centerX = (canvas.width - panelWidth) / 2;
		const centerY = (canvas.height - panelHeight) / 2;
		this.buttonSettings.forEach(btn => {
			const kaishibtn = {
				x: centerX +btn.x,
				y: centerY + btn.y,
				w: btn1Bg.box.w,
				h: btn1Bg.box.h
			}
			// 点击开始游戏
			if (this.isInRect(x, y, kaishibtn)) {

			}
			this.setShow = true
		})

	}

	render() {
		if (!this.setShow) { return }
		this.setShow = false
		const canvas = GameGlobal.databus.canvas
		const ctx = GameGlobal.databus.ctx
		// 绘制半透明背景
		ctx.fillStyle = 'rgba(0, 0, 0, 0.6)';
		ctx.fillRect(0, 0, canvas.width, canvas.height);
		// 面板尺寸调整为原来的60%
		const panelWidth = this.setBg.w;
		const panelHeight = this.setBg.h;
		const centerX = (canvas.width - panelWidth) / 2;
		const centerY = (canvas.height - panelHeight) / 2;
		// 绘制主面板
		ctx.save();
		// 背景带立体感
		ctx.shadowColor = 'rgba(0, 0, 0, 0.3)';
		ctx.shadowOffsetX = 5;
		ctx.shadowOffsetY = 5;
		ctx.shadowBlur = 10;
		ctx.beginPath();
		this.drawRoundedRectComplex(ctx,centerX, centerY, panelWidth, panelHeight, 15)
		// ctx.roundRect(centerX, centerY, panelWidth, panelHeight, 15);
		const bgGradient = ctx.createLinearGradient(0, centerY, 0, centerY + panelHeight);
		bgGradient.addColorStop(0, '#f8f9ff');
		bgGradient.addColorStop(1, '#e0e5ff');
		ctx.fillStyle = bgGradient;
		ctx.fill();

		// 标题文字
		ctx.shadowColor = 'rgba(0, 0, 0, 0.15)';
		ctx.font = 'bold 36px Arial';
		ctx.fillStyle = '#2d3436';
		ctx.textAlign = 'center';
		ctx.fillText('设 置', centerX + panelWidth / 2, centerY + 55);

		// 开关按钮配置（y坐标相对面板顶部）

		// 绘制开关组
		this.buttonSettings.forEach(btn => {
			const btnY = centerY + btn.y;
			// 文字标签
			ctx.font = '26px "Microsoft Yahei"';
			ctx.fillStyle = '#4a4a4a';
			ctx.textAlign = 'left';
			ctx.fillText(btn.label, centerX + 35, btnY + 28);

			// 开关背景（尺寸缩小）
			const switchWidth = btn.w;
			const switchHeight = btn.h;
			ctx.beginPath();
			console.log("尺寸", centerX, panelWidth)
			this.drawRoundedRectComplex(ctx,centerX + btn.x, btnY, switchWidth, switchHeight, 20)
			// ctx.roundRect(centerX + btn.x, btnY, switchWidth, switchHeight, 20);
			const switchGradient = ctx.createLinearGradient(0, btnY, 0, btnY + switchHeight);
			switchGradient.addColorStop(0, btn.state ? '#4cd964' : '#e6e6e6');
			switchGradient.addColorStop(1, btn.state ? '#34c759' : '#d1d1d1');
			ctx.fillStyle = switchGradient;
			ctx.fill();

			// 开关滑块（更精致的样式）
			ctx.shadowColor = 'rgba(0, 0, 0, 0.1)';
			ctx.shadowOffsetX = 2;
			ctx.shadowOffsetY = 2;
			ctx.beginPath();
			ctx.arc(
				centerX + btn.x + (btn.state ? 55 : 25),
				btnY + switchHeight / 2,
				15,
				0,
				Math.PI * 2
			);
			ctx.fillStyle = '#ffffff';
			ctx.fill();
		});

		// 退出按钮（更紧凑的样式）
		const exitBtn = {
			y: centerY + 320,
			height: 50,
			width: 160
		};

		ctx.beginPath();
		this.drawRoundedRectComplex(ctx,centerX + (panelWidth - exitBtn.width) / 2,
		exitBtn.y,
		exitBtn.width,
		exitBtn.height,
		25)
		// ctx.roundRect(
		// 	centerX + (panelWidth - exitBtn.width) / 2,
		// 	exitBtn.y,
		// 	exitBtn.width,
		// 	exitBtn.height,
		// 	25
		// );
		const exitGradient = ctx.createLinearGradient(0, exitBtn.y, 0, exitBtn.y + exitBtn.height);
		exitGradient.addColorStop(0, '#ff7675');
		exitGradient.addColorStop(1, '#ff6b6b');
		ctx.fillStyle = exitGradient;
		ctx.fill();

		// 按钮文字
		ctx.textAlign = 'center';
		ctx.font = 'bold 28px Arial';
		ctx.fillStyle = 'white';
		ctx.fillText('返 回', centerX + panelWidth / 2, exitBtn.y + 35);

		ctx.restore();
	}

	drawRoundedRectComplex(ctx, x, y, w, h, radii) {
		ctx.beginPath();
		// 分别设置四个角的半径（顺序：左上、右上、右下、左下）
		ctx.moveTo(x + radii, y);
		ctx.arcTo(x + w, y, x + w, y + radii, radii);
		ctx.arcTo(x + w, y + h, x + w - radii, y + h, radii);
		ctx.arcTo(x, y + h, x, y + h - radii, radii);
		ctx.arcTo(x, y, x + radii, y, radii);
		ctx.closePath();
	}
	// 辅助函数
	isInRect(x, y, rect) {
		return x >= rect.x && x <= rect.x + rect.w &&
			y >= rect.y && y <= rect.y + rect.h;
	}
}